package me.ilich.piet.interpreter;

import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.List;

public class StackState {
	
	public static final int NORTH = 0;
	public static final int EAST = 1;
	public static final int SOUTH = 2;
	public static final int WEST = 3;
	
	public static final int LEFT = 4;
	public static final int RIGHT = 5;
	
	private int mDP = EAST;
	private int mCC = LEFT;
	private final List<Integer> mData = new ArrayList<Integer>();
	
	public List<Integer> getData(){
		return mData;
	}
		
	public int getDP(){
		return mDP;
	}
	
	public void setDP(int value){
		if(value!=NORTH && value!=EAST && value!=SOUTH && value!=WEST) throw new InvalidParameterException("value");
		mDP = value;
	}
	
	public void rotateClockwise(){
		switch (mDP) {
		case NORTH:
			mDP = EAST;
			break;
		case EAST:
			mDP = SOUTH;
			break;
		case SOUTH:
			mDP = WEST;
			break;
		case WEST:
			mDP = NORTH;
			break;			
		default:
			throw new RuntimeException("invalid dp");
		}
	}
	
	public void rotateAnticlockwise(){
		switch (mDP) {
		case NORTH:
			mDP = WEST;
			break;
		case EAST:
			mDP = NORTH;
			break;
		case SOUTH:
			mDP = EAST;
			break;
		case WEST:
			mDP = SOUTH;
			break;			
		default:
			throw new RuntimeException("invalid dp");
		}		
	}
	
	public void rotate(int times){
		if(times>0){
			for(int i=0; i<times; i++){
				rotateClockwise();
			}
		}else{
			for(int i=times; i<0; i++){
				rotateAnticlockwise();
			}			
		}
	}
	
	public void turn(){
		switch (mCC) {
		case LEFT:
			mCC = RIGHT;
			break;
		case RIGHT:
			mCC = LEFT;
			break;
		default:
			throw new RuntimeException("invalid cc");
		}
	}
	
	public void turn(int times){
		for(int i=0; i<Math.abs(times); i++){
			turn();
		}
	}
	
	public int getCC(){
		return mCC;
	}
	
	public void setCC(int value){
		if(value!=LEFT && value!=RIGHT) throw new InvalidParameterException("value");
		mCC = value;
	}

}
